package com.hwd.srb.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hwd.srb.core.mapper.TransFlowMapper;
import com.hwd.srb.core.pojo.bo.TransFlowBO;
import com.hwd.srb.core.pojo.entity.TransFlow;
import com.hwd.srb.core.pojo.entity.UserInfo;
import com.hwd.srb.core.service.TransFlowService;
import com.hwd.srb.core.service.UserInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 交易流水表 服务实现类
 * </p>
 *
 * @author 黄伟东/Victor
 * @since 2022-03-10
 */
@Service
public class TransFlowServiceImpl extends ServiceImpl<TransFlowMapper, TransFlow> implements TransFlowService {

    @Resource
    private UserInfoService userInfoService;

    /**
     * 添加交易流水记录
     *
     * @param transFlowBO 交易流水对象
     */
    @Transactional
    @Override
    public void saveTransFlow(TransFlowBO transFlowBO) {

        // 根据bindCode获取userId
        UserInfo userInfo = userInfoService.getByBindCode(transFlowBO.getBindCode());

        // 创建交易流水表对象
        TransFlow transFlow = new TransFlow();

        // 添加transFlowBO数据
        transFlow.setMemo(transFlowBO.getMemo());
        transFlow.setTransNo(transFlowBO.getAgentBillNo());
        transFlow.setTransType(transFlowBO.getTransTypeEnum().getTransType());
        transFlow.setTransTypeName(transFlowBO.getTransTypeEnum().getTransTypeName());
        transFlow.setTransAmount(transFlowBO.getAmount());

        // 添加基本信息
        transFlow.setUserId(userInfo.getId());
        transFlow.setUserName(userInfo.getName());

        // 执行添加操作
        baseMapper.insert(transFlow);

    }

    /**
     * 判断该充值订单号是否已经交易过
     *
     * @param agentBillNo 商户充值订单号
     * @return boolean
     */
    @Override
    public boolean isSaveTransFlow(String agentBillNo) {
        LambdaQueryWrapper<TransFlow> queryWrapper = new LambdaQueryWrapper<TransFlow>()
                .eq(TransFlow::getTransNo, agentBillNo);
        Integer count = baseMapper.selectCount(queryWrapper);
        return count > 0;
    }
}
